﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;

public partial class UserControls_ScoreCardControl : System.Web.UI.UserControl
{
    public DataSet dsQustn;
    public int questionid, ismultipleoption,pos;
    ListItem lst = new ListItem();


    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public void fillQuestionDs(int testId)
    {
        dsQustn = DALKAT.GetQuestions(testId);
        dsQustn.Tables[0].Columns.Add("QuestionNo", typeof(System.Int32));
        for (int i = 0; i < dsQustn.Tables[0].Rows.Count; i++)
            dsQustn.Tables[0].Rows[i]["QuestionNo"] = i + 1;
    }

    public void displayQuestion(int pos, string attemptId)
    {
        lablQuestion.Text = dsQustn.Tables[0].Rows[pos][1].ToString();
        questionid = Int32.Parse(dsQustn.Tables[0].Rows[pos][2].ToString());
        ismultipleoption = Int32.Parse(dsQustn.Tables[0].Rows[pos][3].ToString());
        lablQuestionNo.Text = (Convert.ToInt32(dsQustn.Tables[0].Rows[pos]["QuestionNo"])).ToString();
        displayOption(questionid);
    }

    private void displayOption(int questionid)
    {
        DataSet dsoptn = DALKAT.GetOptions(questionid);
        rdoOptions.Items.Clear();
        chkOptions.Items.Clear();

        if (ismultipleoption == 0)
        {
            for (int i = 0; i < dsoptn.Tables[0].Rows.Count; i++)
            {
                lst = new ListItem();
                lst.Text = dsoptn.Tables[0].Rows[i][1].ToString();
                lst.Value = dsoptn.Tables[0].Rows[i][0].ToString();
                rdoOptions.Items.Add(lst);
            }
        }
        else
        {
            for (int i = 0; i < dsoptn.Tables[0].Rows.Count; i++)
            {
                lst = new ListItem();
                lst.Text = dsoptn.Tables[0].Rows[i][1].ToString();
                lst.Value = dsoptn.Tables[0].Rows[i][0].ToString();
                chkOptions.Items.Add(lst);
            }
        }
        displayUserChoice(questionid);

    }

    private void displayUserChoice(int questionid)
    {
        DataTable dt = new DataTable();
        dt = AdminCreateTestDAL.GetTestAttemptQuestionOption(Convert.ToInt32(Session["AttemptId"]));
        if (ismultipleoption == 0)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (questionid == Convert.ToInt32(dt.Rows[i][1]))
                    rdoOptions.SelectedIndex = rdoOptions.Items.IndexOf(rdoOptions.Items.FindByValue(dt.Rows[i][2].ToString()));
            }

        }
        else
        {
            for (int k = 0; k < dt.Rows.Count; k++)
            {
                if (questionid == Convert.ToInt32(dt.Rows[k][1]))
                {
                    string options = dt.Rows[k][2].ToString();
                    string[] option;

                    option = options.Split('|');

                    for (int i = 0; i < chkOptions.Items.Count; i++)
                    {
                        for (int j = 0; j < option.Length ; j++)
                        {
                            if (chkOptions.Items[i].Value == option[j])
                                chkOptions.Items[i].Selected = true;
                        }
                    }
                }
            }

        }
        displayAnswer(questionid);
        IsCorrect(questionid);
    }

    private void IsCorrect(int questionid)
    {
        DataTable dtUserAttemptQuestionOption = new DataTable();
        dtUserAttemptQuestionOption = AdminCreateTestDAL.GetTestAttemptQuestionOption(Convert.ToInt32(Session["AttemptId"]));
        for (int i = 0; i < dtUserAttemptQuestionOption.Rows.Count; i++)
        {
            if (questionid == Convert.ToInt32(dtUserAttemptQuestionOption.Rows[i][1]))
            {
                DataTable dtAnswer = new DataTable();
                dtAnswer = DALKAT.GetRightOptionId(questionid);
                if (dtAnswer.Rows[0][0].ToString() == dtUserAttemptQuestionOption.Rows[i][2].ToString())
                {
                    lablRight.ForeColor = System.Drawing.Color.Green;
                    lablWrong.ForeColor = System.Drawing.Color.Black;
                    lablWrong.Visible = false;
                    lablRight.Visible = true;
                    if (ismultipleoption == 0)
                    {
                        for (int k = 0; k < rdoOptions.Items.Count; k++)
                        {
                            if (rdoOptions.Items[k].Selected == true)
                                rdoOptions.Items[k].Attributes.CssStyle.Add("color", "Green");
                        }
                    }
                    else
                    {
                        for (int k = 0; k < chkOptions.Items.Count; k++)
                        {
                            if (chkOptions.Items[k].Selected == true)
                                chkOptions.Items[k].Attributes.CssStyle.Add("color", "Green");
                        }
                    }

                    lablAns.Visible = false;
                    lablAnswer.Visible = false;
                    labljust.Visible = false;
                    lablJustification.Visible = false;
                }
                else
                {
                    lablRight.ForeColor = System.Drawing.Color.Black;
                    lablWrong.ForeColor = System.Drawing.Color.Red;
                    lablWrong.Visible = true;
                    lablRight.Visible = false;
                    if (ismultipleoption == 0)
                    {
                        for (int k = 0; k < rdoOptions.Items.Count; k++)
                        {
                            if (rdoOptions.Items[k].Selected == true)
                                rdoOptions.Items[k].Attributes.CssStyle.Add("color", "Red");
                        }
                    }
                    else
                    {
                        for (int k = 0; k < chkOptions.Items.Count; k++)
                        {
                            if (chkOptions.Items[k].Selected == true)
                                chkOptions.Items[k].Attributes.CssStyle.Add("color", "Red");
                        }
                    }

                    lablAns.Visible = true;
                    lablAnswer.Visible = true;
                    labljust.Visible = true;
                    lablJustification.Visible = true;
                }
            }
        }
    }

    private void displayAnswer(int questionid)
    {
        DataTable dtAnswer = new DataTable();
        dtAnswer = DALKAT.GetAnswer(questionid);
        if (dtAnswer.Rows[0][0].ToString().Contains('|'))
            dtAnswer.Rows[0][0] = dtAnswer.Rows[0][0].ToString().Replace('|', ',');
        lablAnswer.Text = dtAnswer.Rows[0][0].ToString();
        lablJustification.Text = dtAnswer.Rows[0][1].ToString();
    }
}